﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package GameMode {
    import Engine.Classes.*;
    import flash.events.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Mechanics.GameMechanicInterfaces.*;
    import Mechanics.*;
    import Events.*;
    import Classes.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import tool.*;
    import Display.*;
    import Classes.doobers.*;
    import Modules.guide.*;
    import Engine.*;
    import com.adobe.utils.*;

    public class GMDefault extends GameMode {

        protected const CANCEL_WARNING_NUM:int = 1;
        protected const BORDER_COLOR:Number = 0;
        protected const BORDER_ALPHA:Number = 1;
        protected const PATH_COLOR:Number = 0xFFAA00;
        protected const PATH_ALPHA:Number = 1;

        protected var m_viewDragStartPos:Point = null;
        protected var m_dragging:Boolean = false;
        protected var m_highlightedObject:GameObject = null;
        protected var m_selectedObject:GameObject = null;
        protected var m_currentCursor:Object = null;
        protected var m_moveToPos:Point;
        protected var m_occludingObjects:Array;

        public function GMDefault(){
            this.m_occludingObjects = [];
            super();
        }
        public function setVisit(_arg1:String):void{
            Global.setVisiting(_arg1);
        }
        override public function enableMode():void{
            var _local2:MapResource;
            super.enableMode();
            var _local1:Array = Global.world.getObjectsByClass(MapResource);
            for each (_local2 in _local1) {
                _local2.onEditModeSwitch();
            };
        }
        override public function disableMode():void{
            this.dehighlightObject();
            super.disableMode();
        }
        override public function removeMode():void{
            if (((this.m_selectedObject) && ((this.m_selectedObject is IMechanicUser)))){
                MechanicManager.getInstance().handleAction((this.m_selectedObject as IMechanicUser), Global.getClassName(this));
            };
            super.removeMode();
            this.deselectObject();
        }
        protected function getSelectableTypes():int{
            return (Constants.WORLDOBJECT_ALL);
        }
        override public function onMouseDown(_arg1:MouseEvent):Boolean{
            var _local3:Boolean;
            super.onMouseDown(_arg1);
            if (this.isViewDraggingEnabled()){
                this.m_viewDragStartPos = GlobalEngine.viewport.getScrollPosition().clone();
                this.m_dragging = false;
            };
            var _local2:GameObject = this.m_highlightedObject;
            if ((((_local2 is LotSite)) && (Global.isVisiting()))){
                _local3 = this.checkLotOrderAvailability();
                this.checkLotSite(_local2);
            };
            if (((_local2) && (this.isObjectSelectable(_local2)))){
                this.selectObject(_local2, _arg1);
            } else {
                this.deselectObject();
            };
            if (((this.m_highlightedObject) && (this.m_highlightedObject.isAttached()))){
                this.m_highlightedObject.onMouseOut();
            };
            return (true);
        }
        protected function checkLotOrderAvailability():Boolean{
            return (Global.world.citySim.lotManager.checkLotOrderAvailablity());
        }
        protected function checkLotSite(_arg1:GameObject):void{
            var _local4:Vector.<Quest>;
            var _local5:Boolean;
            var _local6:Quest;
            var _local7:Point;
            var _local8:Point;
            if (Global.world.ownerId == Player.FAKE_USER_ID_STRING){
                _local4 = Global.questManager.getIncompleteActiveQuests();
                _local5 = false;
                for each (_local6 in _local4) {
                    if (_local6.name == "q_expanding_ventures"){
                        _local5 = true;
                        break;
                    };
                };
                if (!_local5){
                    UI.displayMessage(ZLoc.t("Dialogs", "NotReadyForFranchises"));
                    return;
                };
            };
            var _local2 = (Global.franchiseManager.model.getBusinessCount() > 0);
            var _local3:Boolean = this.checkLotOrderAvailability();
            if ((((_arg1 is LotSite)) && (!(Global.franchiseManager.placementMode)))){
                if (!_local2){
                    UI.displayMessage(ZLoc.t("Dialogs", "HasBusinessCheck"));
                } else {
                    if (!_local3){
                        UI.displayMessage(ZLoc.t("Dialogs", "LotOrderAvailableCheck"));
                    } else {
                        m_selectedLotSite = (_arg1 as LotSite);
                        _local7 = new Point(160, 160);
                        _local8 = new Point((_arg1.getPosition().x + (_arg1.getSize().x / 2)), (_arg1.getPosition().y + (_arg1.getSize().y / 2)));
                        Global.guide.displaySpotLightOnTile(_local8, _local7, GuideConstants.MASK_STYLE_ELLIPSE, GuideConstants.MASK_LOT_SITES, 0.3, false);
                        UI.displayCatalog("business", null);
                        Global.franchiseManager.placementMode = true;
                    };
                };
            } else {
                Global.franchiseManager.placementMode = false;
            };
        }
        override public function onMouseUp(_arg1:MouseEvent):Boolean{
            if (((this.m_highlightedObject) && (this.m_highlightedObject.isAttached()))){
                this.m_highlightedObject.onMouseOver(_arg1);
            };
            if (this.m_dragging == false){
                this.handleClick(_arg1);
            };
            var _local2:Point = new Point(_arg1.stageX, _arg1.stageY);
            this.setHighlightObjectFromPoint(_local2);
            this.updateCursor();
            this.finishMouseUpEvent();
            super.onMouseUp(_arg1);
            return (true);
        }
        protected function finishMouseUpEvent():void{
            this.m_dragging = false;
            this.m_viewDragStartPos = null;
        }
        protected function canBeClicked(_arg1:MouseEvent):Boolean{
            return (true);
        }
        protected function isViewDraggingEnabled():Boolean{
            return (!(Global.guide.isActive()));
        }
        protected function handleClick(_arg1:MouseEvent):void{
            var _local2:Boolean;
            if (this.m_highlightedObject){
                _local2 = true;
                if (((this.m_selectedObject) && ((this.m_selectedObject is IMechanicUser)))){
                    _local2 = !(MechanicManager.getInstance().handleAction((this.m_selectedObject as IMechanicUser), Global.getClassName(this)));
                };
                if (((((_local2) && (this.canBeClicked(_arg1)))) && ((this.m_highlightedObject.isAttached() == true)))){
                    this.m_highlightedObject.onClick();
                };
                if (((this.m_highlightedObject) && ((this.m_highlightedObject.isAttached() == false)))){
                    this.dehighlightObject();
                };
            };
        }
        override public function onMouseMove(_arg1:MouseEvent):Boolean{
            var _local5:Vector2;
            var _local6:Vector2;
            var _local2:Number = ((_arg1)!=null) ? _arg1.stageX : Global.stage.mouseX;
            var _local3:Number = ((_arg1)!=null) ? _arg1.stageY : Global.stage.mouseY;
            var _local4:Point = new Point(_local2, _local3);
            if (((((((this.m_viewDragStartPos) && (_local4))) && (m_mouseDownPos))) && ((_local4.subtract(m_mouseDownPos).length >= Constants.DRAG_CLICK_EPSILON)))){
                this.m_dragging = true;
            };
            if (((this.m_dragging) && (this.m_viewDragStartPos))){
                _local5 = new Vector2();
                _local5.x = (_local2 - m_mouseDownPos.x);
                _local5.y = (_local3 - m_mouseDownPos.y);
                _local6 = new Vector2();
                _local6.x = (this.m_viewDragStartPos.x + (_local5.x / GlobalEngine.viewport.getZoom()));
                _local6.y = (this.m_viewDragStartPos.y + (_local5.y / GlobalEngine.viewport.getZoom()));
                _local6 = limitBackgroundScrolling(_local6);
                GlobalEngine.viewport.setScrollPosition(_local6);
                if (!OptimizationConfig.optimizeCulling){
                    Global.world.updateWorldObjectCulling(GameWorld.CULL_CHECK_ALL);
                };
            } else {
                this.setHighlightObjectFromPoint(_local4);
            };
            this.updateCursor();
            this.updateOverlay();
            return (true);
        }
        public function updateObjectUnderMouse():void{
            var _local1:Point = new Point(Global.stage.mouseX, Global.stage.mouseY);
            this.setHighlightObjectFromPoint(_local1);
            this.updateCursor();
            this.updateOverlay();
        }
        protected function getObjectListFromPoint(_arg1:Point):Array{
            var _local5:WorldObject;
            var _local2:Point = IsoMath.stageToViewport(_arg1);
            var _local3:Array = Global.world.getPickObjects(_local2, this.getSelectableTypes());
            var _local4:Array = Global.world.getCollisionMap().getObjectsByPosition(Math.floor(getMouseTilePos().x), Math.floor(getMouseTilePos().y));
            for each (_local5 in _local4) {
                if ((((_local5 is MapResource)) && ((_local5 as MapResource).usingTilePicking()))){
                    if (!ArrayUtil.arrayContainsValue(_local3, _local5)){
                        _local3.push(_local5);
                    };
                };
            };
            return (_local3);
        }
        protected function setHighlightObjectFromPoint(_arg1:Point):void{
            var _local5:int;
            var _local2:Array = this.getObjectListFromPoint(_arg1);
            var _local3:GameObject;
            if (_local2.length){
                _local5 = (_local2.length - 1);
                while (_local5 >= 0) {
                    if (this.isObjectHighlightable(_local2[_local5])){
                        _local3 = _local2[_local5];
                    };
                    _local5--;
                };
                if (!_local3){
                    _local3 = _local2[0];
                };
            };
            m_displayObject.graphics.clear();
            if ((((_local3 is MapResource)) && (this.isObjectHighlightable(_local3)))){
                this.drawFlatTileOutline((_local3 as MapResource));
            };
            var _local4:Boolean = Boolean(((_local3) && (this.isObjectHighlightable(_local3))));
            if (_local4){
                this.highlightObject(_local3);
            } else {
                if (_local3 == null){
                    this.dehighlightObject();
                };
            };
            if (OffsetEditor.active){
                OffsetEditor.instance.consider(_local3);
            };
        }
        protected function drawFlatTileOutline(_arg1:MapResource):void{
            var _local2:Vector3;
            if (_arg1.usingTileHighlight()){
                _local2 = _arg1.getSize();
                m_displayObject.graphics.lineStyle(1, _arg1.getHighlightColor());
                m_displayObject.graphics.beginFill(0, 0);
                drawTileArea(m_displayObject.graphics, new Point(_arg1.getPosition().x, _arg1.getPosition().y), _local2.x, _local2.y);
                m_displayObject.graphics.endFill();
            };
        }
        protected function isObjectHighlightable(_arg1:GameObject):Boolean{
            if ((((_arg1 is Doober)) && ((_arg1 as Doober).isFlying))){
                return (false);
            };
            return (true);
        }
        protected function isObjectSelectable(_arg1:GameObject):Boolean{
            return (true);
        }
        protected function isObjectDraggable(_arg1:GameObject):Boolean{
            return (_arg1.canBeDragged());
        }
        public function turnOffObject():void{
            this.dehighlightObject();
        }
        protected function highlightedObjectStateChangeHandler(_arg1:GameObjectEvent):void{
            this.updateCursor();
        }
        protected function highlightObject(_arg1:GameObject, _arg2:MouseEvent=null):void{
            var _local3:MapResource;
            if (_arg1 != this.m_highlightedObject){
                this.dehighlightObject();
                this.m_highlightedObject = _arg1;
                this.m_highlightedObject.addEventListener(GameObjectEvent.STATE_CHANGE, this.highlightedObjectStateChangeHandler, false, 0, true);
                if (_arg1){
                    _arg1.onMouseOver(_arg2);
                };
                this.updateCursor();
            };
            if (_arg1 != null){
                _local3 = (_arg1 as MapResource);
                if (((_local3) && (_local3.isUpgradePossible()))){
                    _local3.refreshArrow();
                };
                Global.ui.showToolTip(_arg1);
            };
        }
        protected function dehighlightObject():void{
            Global.ui.hideToolTip(this.m_highlightedObject);
            if (this.m_highlightedObject){
                this.m_highlightedObject.removeEventListener(GameObjectEvent.STATE_CHANGE, this.highlightedObjectStateChangeHandler);
                this.m_highlightedObject.onMouseOut();
                this.m_highlightedObject = null;
            };
            this.updateCursor();
        }
        protected function selectObject(_arg1:GameObject, _arg2:MouseEvent=null):void{
            if (_arg1 != this.m_selectedObject){
                this.deselectObject();
                this.m_selectedObject = _arg1;
                if (_arg1){
                    _arg1.onSelect();
                };
            };
        }
        protected function deselectObject():void{
            if (this.m_selectedObject){
                this.m_selectedObject.onDeselect();
                this.m_selectedObject = null;
            };
        }
        protected function updateCursor():void{
            var _local1:Object = this.getCursor();
            if (_local1 != this.m_currentCursor){
                if (m_cursorId > 0){
                    UI.removeCursor(m_cursorId);
                    m_cursorId = 0;
                };
                m_cursorId = UI.setCursor(_local1, true);
                this.m_currentCursor = _local1;
            };
        }
        protected function getCursor():Object{
            var _local1:Object;
            if (((this.m_highlightedObject) && (this.m_highlightedObject.overrideCursor()))){
                _local1 = (GameObject.USE_CUSTOM_CURSORS) ? this.m_highlightedObject.getCustomCursor() : null;
                if (!_local1){
                    _local1 = this.m_highlightedObject.getCursor();
                };
            };
            if (_local1 == null){
                _local1 = m_cursorImage;
            };
            return (_local1);
        }
        override public function onMouseWheel(_arg1:MouseEvent):Boolean{
            var _local2:Number;
            var _local3:int;
            var _local4:Vector2;
            if (Global.world.isZoomEnabled()){
                _local2 = GlobalEngine.viewport.getZoom();
                if (_arg1.delta > 1){
                    _local2 = (_local2 + Global.gameSettings.getNumber("zoomStep"));
                    _local3 = GameWorld.CULL_ZOOM_IN;
                } else {
                    if (_arg1.delta < -1){
                        _local2 = (_local2 - Global.gameSettings.getNumber("zoomStep"));
                        _local3 = GameWorld.CULL_ZOOM_OUT;
                    };
                };
                _local2 = Math.min(Global.gameSettings.getNumber("maxZoom"), _local2);
                _local2 = Math.max(Global.gameSettings.getNumber("minZoom"), _local2);
                if (GlobalEngine.viewport.getZoom() == _local2){
                    return (true);
                };
                GlobalEngine.viewport.setZoom(_local2);
                _local4 = GlobalEngine.viewport.getScrollPosition();
                _local4 = GameMode.limitBackgroundScrolling(_local4);
                GlobalEngine.viewport.setScrollPosition(_local4);
                Global.world.updateWorldObjectCulling(_local3);
            };
            return (true);
        }
        protected function updateOverlay():void{
        }

    }
}//package GameMode 
